package com.mine.eureka.controllers;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ProductorConfiguration {

    static  private final Logger logger = LoggerFactory.getLogger(ProductorConfiguration.class);

    @Value("${rocketmq.name-server}")
    public String nameserver;

    @Value("${rocketmq.producer.group}")
    public String group;

    @Bean(name = "_mqProducer")
    public  DefaultMQProducer createBean(){
        DefaultMQProducer producer = null;
        try {
            producer = new DefaultMQProducer(group);
            producer.setNamesrvAddr(nameserver);
            producer.start();
            producer.setRetryTimesWhenSendAsyncFailed(3);;
            producer.setVipChannelEnabled(false);


            logger.info("消息队列生产者连接成功");
        } catch (Exception ex) {
            logger.error(ex.getMessage(),ex);
        }
        return producer;
    }
}
